<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>Android Studio上lint的使用 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Android Studio上lint的使用</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">June 20, 2016&nbsp;&nbsp;20:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Android-%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/">Android-工具使用</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <p>Android Studio作为Google官方推荐的IDE，功能非常强大，其中提供了一套静态代码分析工具，能检查安卓项目的源文件，从而查找潜在的程序错误以及优化提升的方案。它可以检查出： xml文件中是否存在hardcode硬编码、unused resources没有使用到的资源、probable bug可能的bug等。诚然，Lint 能在诸多方面提供帮助，包括：正确性，安全，性能，易用性，可用性，国际化等等。<br>下面是它的使用步骤：</p>
<h3 id="1．找到Analyze目录下的Inspect-Code检查代码选项点击"><a href="#1．找到Analyze目录下的Inspect-Code检查代码选项点击" class="headerlink" title="1．找到Analyze目录下的Inspect Code检查代码选项点击"></a>1．找到Analyze目录下的Inspect Code检查代码选项点击</h3><p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/1.png?raw=true" alt=""></p>
<h3 id="2-然后弹出下面这个框框，在这个列表选项中我们可以选择Inspect-Code的范围，点击OK"><a href="#2-然后弹出下面这个框框，在这个列表选项中我们可以选择Inspect-Code的范围，点击OK" class="headerlink" title="2. 然后弹出下面这个框框，在这个列表选项中我们可以选择Inspect Code的范围，点击OK"></a>2. 然后弹出下面这个框框，在这个列表选项中我们可以选择Inspect Code的范围，点击OK</h3><p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/2.png?raw=true" alt=""></p>
<h3 id="3-待分析完毕后，我们可以在Inspection栏目中看到检查的结果"><a href="#3-待分析完毕后，我们可以在Inspection栏目中看到检查的结果" class="headerlink" title="3. 待分析完毕后，我们可以在Inspection栏目中看到检查的结果"></a>3. 待分析完毕后，我们可以在Inspection栏目中看到检查的结果</h3><p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/3.png?raw=true" alt=""></p>
<h3 id="4-自定义Android-Lint的检查提示"><a href="#4-自定义Android-Lint的检查提示" class="headerlink" title="4. 自定义Android Lint的检查提示"></a>4. 自定义Android Lint的检查提示</h3><p>我们知道在xml文件中编写布局时候，假如我们在一个TextView中的text属性上直接写字符串，在textSize属性上写入的值用dp为单位，那么，此时AS中将会有建议提示，如：</p>
<p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/4.png?raw=true" alt=""></p>
<p>可以看到这个提示太不明显了，所以我们可以通过更改对应的severity等级来更改提示的等级，如：<br>默认hardcode的severity等级为warning，我们修改hardcode的severity等级为error，那么在存在硬编码时候将会以error等级提醒我们： </p>
<p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/5.png?raw=true" alt=""></p>
<p>再修改testsize如果使用dp为单位的severity等级为： </p>
<p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/6.png?raw=true" alt=""></p>
<p>修改完成后，我们可以看到提示使用红色的波浪线标记了，更直观了，如： </p>
<p><img src="https://github.com/zhangpingV5/BlogPic/blob/master/lint%E7%9A%84%E4%BD%BF%E7%94%A8/6.png?raw=true" alt=""></p>
<p>而且，Android Lint中还有很多可以自己去设置的，这个只是起提示建议作用，并不会影响程序的正常运行。</p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2016/06/20/Android%20Studio%E4%B8%8Alint%E7%9A%84%E4%BD%BF%E7%94%A8/">https://zhangpingv5.gitee.io/2016/06/20/Android%20Studio%E4%B8%8Alint%E7%9A%84%E4%BD%BF%E7%94%A8/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/lint/"># lint</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2016/06/21/%E8%87%AA%E5%AE%9A%E4%B9%89Lint/">自定义Lint</a>
            
            
            <a class="next" rel="next" href="/2016/04/21/andfix%E6%B5%85%E6%9E%90%E4%B8%8E%E4%BD%BF%E7%94%A8/">andfix浅析与使用</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
